est clear
use "${filedata}for_regs.dta", clear

//////////////////////////
//////IV
///////////////////////////

//////////////////DV: initial amendment//////////////////////////////

foreach i in prop_female half_female all_female  {

	replace Female=`i'
	replace iv=iv_initial
	replace Female_iv=iv_initial_`i'
	replace Female_initial_rejection= `i'_initial_rejection

ivreghdfe initial_amendment (c.Female#c.initial_rejection initial_rejection =  c.Female#c.iv iv) Female ///
inventor_count prev_apps prev_pats min_years pub_clm_ct pub_wrd_ct pub_wrd_min pub_dep_clm_ct pub_dep_wrd_ct pub_dep_wrd_min pub_wrd_avg pub_dep_wrd_avg  ///  
m_pub_clm_ct m_prev_pats m_min_years ///
, a(art_year) cluster(examiner_year) ffirst


	eststo iv_ia_`i'
	estadd scalar  clus=e(N_clust) : iv_ia_`i'
	sum initial_amendment if e(sample)
	estadd scalar  mean=r(mean): iv_ia_`i'
	estadd scalar fstat= e(first)[4,1]
	estadd local control="X" :  iv_ia_`i'
	
	if "`i'"=="half_female" {
	estadd local Fem="Half Female": iv_ia_`i'
	} 
	else if "`i'"=="all_female" {
	estadd local Fem="All Female": iv_ia_`i'
	}
	else if "`i'"=="prop_female" {
	estadd local Fem="Proportion": iv_ia_`i'
	} 


}	

//SOLO
	replace Female=all_female
	replace Female_initial_rejection= all_female_initial_rejection
	replace iv=iv_initial
	replace Female_iv=iv_initial_all_female	

ivreghdfe initial_amendment (c.Female#c.initial_rejection initial_rejection =  c.Female#c.iv iv) Female ///
prev_apps prev_pats min_years pub_clm_ct pub_wrd_ct pub_wrd_min pub_dep_clm_ct pub_dep_wrd_ct pub_dep_wrd_min pub_wrd_avg pub_dep_wrd_avg  ///  
m_pub_clm_ct m_prev_pats m_min_years ///
if solo==1 , a(art_year) cluster(examiner_year) ffirst


	eststo iv_ia_solo
	estadd scalar  clus=e(N_clust) : iv_ia_solo
	sum initial_amendment if e(sample)
	estadd scalar  mean=r(mean): iv_ia_solo
	estadd scalar fstat= e(first)[4,1]
	estadd local control="X" :  iv_ia_solo
	estadd local Fem="Solo": iv_ia_solo
	
//////////////////DV: patent granted//////////////////////////////

foreach i in prop_female half_female all_female  {

	replace Female=`i'
	replace iv=iv_initial
	replace Female_iv=iv_initial_`i'
	replace Female_initial_rejection= `i'_initial_rejection

ivreghdfe patent_issued (c.Female#c.initial_rejection initial_rejection =  c.Female#c.iv iv) Female ///
inventor_count prev_apps prev_pats min_years pub_clm_ct pub_wrd_ct pub_wrd_min pub_dep_clm_ct pub_dep_wrd_ct pub_dep_wrd_min pub_wrd_avg pub_dep_wrd_avg  ///  
m_pub_clm_ct m_prev_pats m_min_years ///
, a(art_year) cluster(examiner_year) ffirst


	eststo iv_pg_`i'
	estadd scalar  clus=e(N_clust) : iv_pg_`i'
	sum patent_issued if e(sample)
	estadd scalar  mean=r(mean): iv_pg_`i'
	estadd scalar fstat= e(first)[4,1]
	estadd local control="X" :  iv_pg_`i'
	
	if "`i'"=="half_female" {
	estadd local Fem="Half Female": iv_pg_`i'
	} 
	else if "`i'"=="all_female" {
	estadd local Fem="All Female": iv_pg_`i'
	}
	else if "`i'"=="prop_female" {
	estadd local Fem="Proportion": iv_pg_`i'
	} 


}	

//solo

	replace Female=all_female
	replace Female_initial_rejection= all_female_initial_rejection
	replace iv=iv_initial
	replace Female_iv=iv_initial_all_female

ivreghdfe patent_issued (c.Female#c.initial_rejection initial_rejection =  c.Female#c.iv iv) Female ///
prev_apps prev_pats min_years pub_clm_ct pub_wrd_ct pub_wrd_min pub_dep_clm_ct pub_dep_wrd_ct pub_dep_wrd_min pub_wrd_avg pub_dep_wrd_avg  ///  
m_pub_clm_ct m_prev_pats m_min_years ///
if solo==1 , a(art_year) cluster(examiner_year) ffirst


	eststo iv_pg_solo
	estadd scalar  clus=e(N_clust) : iv_pg_solo
	sum patent_issued if e(sample)
	estadd scalar  mean=r(mean): iv_pg_solo
	estadd scalar fstat= e(first)[4,1]
	estadd local control="X" :  iv_pg_solo
	estadd local Fem="Solo": iv_pg_solo
	
/////////////////////////OLS REGS////////////////////////
		  
use "${filedata}for_regs.dta", clear

//////////////////DV: initial amendment//////////////////////////////

foreach i in prop_female half_female all_female  {

	replace Female=`i'
	replace iv=iv_initial
	replace Female_iv=iv_initial_`i'
	replace Female_initial_rejection= `i'_initial_rejection

ivreghdfe initial_amendment c.Female#c.initial_rejection initial_rejection Female ///
inventor_count prev_apps prev_pats min_years pub_clm_ct pub_wrd_ct pub_wrd_min pub_dep_clm_ct pub_dep_wrd_ct pub_dep_wrd_min pub_wrd_avg pub_dep_wrd_avg  ///  
m_pub_clm_ct m_prev_pats m_min_years ///
, a(art_year) cluster(examiner_year) 

	eststo ols_ia_`i'
	estadd scalar  clus=e(N_clust) : ols_ia_`i'
	sum initial_amendment if e(sample)
	estadd scalar  mean=r(mean): ols_ia_`i'
	estadd local control="X" :  ols_ia_`i'
	
	if "`i'"=="half_female" {
	estadd local Fem="Half Female": ols_ia_`i'
	} 
	else if "`i'"=="all_female" {
	estadd local Fem="All Female": ols_ia_`i'
	}
	else if "`i'"=="prop_female" {
	estadd local Fem="Proportion": ols_ia_`i'
	} 


}	

//SOLO
	replace Female=all_female
	replace Female_initial_rejection= all_female_initial_rejection
	replace iv=iv_initial
	replace Female_iv=iv_initial_all_female	

ivreghdfe initial_amendment c.Female#c.initial_rejection initial_rejection Female ///
prev_apps prev_pats min_years pub_clm_ct pub_wrd_ct pub_wrd_min pub_dep_clm_ct pub_dep_wrd_ct pub_dep_wrd_min pub_wrd_avg pub_dep_wrd_avg  ///  
m_pub_clm_ct m_prev_pats m_min_years ///
if solo==1 , a(art_year) cluster(examiner_year) 

	eststo ols_ia_solo
	estadd scalar  clus=e(N_clust) : ols_ia_solo
	sum initial_amendment if e(sample)
	estadd scalar  mean=r(mean): ols_ia_solo
	estadd local control="X" :  ols_ia_solo
	estadd local Fem="Solo": ols_ia_solo
	
//////////////////DV: patent granted//////////////////////////////

foreach i in prop_female half_female all_female  {

	replace Female=`i'
	replace iv=iv_initial
	replace Female_iv=iv_initial_`i'
	replace Female_initial_rejection= `i'_initial_rejection

ivreghdfe patent_issued c.Female#c.initial_rejection initial_rejection Female ///
inventor_count prev_apps prev_pats min_years pub_clm_ct pub_wrd_ct pub_wrd_min pub_dep_clm_ct pub_dep_wrd_ct pub_dep_wrd_min pub_wrd_avg pub_dep_wrd_avg  ///  
m_pub_clm_ct m_prev_pats m_min_years ///
, a(art_year) cluster(examiner_year)

	eststo ols_pg_`i'
	estadd scalar  clus=e(N_clust) : ols_pg_`i'
	sum patent_issued if e(sample)
	estadd scalar  mean=r(mean): ols_pg_`i'
	estadd local control="X" :  ols_pg_`i'
	
	if "`i'"=="half_female" {
	estadd local Fem="Half Female": ols_pg_`i'
	} 
	else if "`i'"=="all_female" {
	estadd local Fem="All Female": ols_pg_`i'
	}
	else if "`i'"=="prop_female" {
	estadd local Fem="Proportion": ols_pg_`i'
	} 


}	

//solo

	replace Female=all_female
	replace Female_initial_rejection= all_female_initial_rejection
	replace iv=iv_initial
	replace Female_iv=iv_initial_all_female


ivreghdfe patent_issued c.Female#c.initial_rejection initial_rejection Female ///
prev_apps prev_pats min_years pub_clm_ct pub_wrd_ct pub_wrd_min pub_dep_clm_ct pub_dep_wrd_ct pub_dep_wrd_min pub_wrd_avg pub_dep_wrd_avg  ///  
m_pub_clm_ct m_prev_pats m_min_years ///
if solo==1 , a(art_year) cluster(examiner_year)

	eststo ols_pg_solo
	estadd scalar  clus=e(N_clust) : ols_pg_solo
	sum patent_issued if e(sample)
	
	estadd scalar  mean=r(mean): ols_pg_solo
	estadd local control="X" :  ols_pg_solo
	estadd local Fem="Solo": ols_pg_solo
	

////////////////////////////////////
////////////WRITE TABLE////////////
////////////////////////////////////

label variable initial_rejection   "Initial Rejection"
label var Female "Female"

esttab  ols_ia_half_female iv_ia_half_female ols_ia_all_female iv_ia_all_female ///
		ols_ia_solo iv_ia_solo ols_ia_prop_female iv_ia_prop_female ///
using "${results}mainregs_quality.tex", replace se(3) b(3) ///
keep(c.Female#c.initial_rejection Female initial_rejection) ///
interaction(" $\times$ ") booktabs nonumbers nomtitles label ///
posthead("& \multicolumn{1}{c}{(1a: OLS)} & \multicolumn{1}{c}{(1b: 2SLS)} & \multicolumn{1}{c}{(2a: OLS)} & \multicolumn{1}{c}{(2b: 2SLS)} & \multicolumn{1}{c}{(3a: 2SLS)} & \multicolumn{1}{c}{(3b: IV)} & \multicolumn{1}{c}{(4a: 2SLS)} & \multicolumn{1}{c}{(4b: IV)} \\ & \multicolumn{2}{c}{Half Female} & \multicolumn{2}{c}{All Female} & \multicolumn{2}{c}{Solo} & \multicolumn{2}{c}{Proportion} \\ \cmidrule(lr){2-3}  \cmidrule(lr){4-5} \cmidrule(lr){6-7}  \cmidrule(lr){8-9} & \multicolumn{8}{c}{Panel A:  Effect of Initial Rejection on Initial Amendment}\\ \cmidrule(lr){2-9}") noobs ///
star(* 0.10 ** 0.05 *** 0.01) 	stat(mean r2_a fstat, fmt(2 2 2) label("Dependent Var. Mean" "Adj. R$^2$" "F-stat")) 

esttab   ols_pg_half_female iv_pg_half_female ols_pg_all_female iv_pg_all_female  ///
		ols_pg_solo iv_pg_solo ols_pg_prop_female iv_pg_prop_female  ///
using "${results}mainregs_quality.tex",  append f se(3) b(3) ///
keep(c.Female#c.initial_rejection Female initial_rejection) ///
interaction(" $\times$ ") booktabs nonumbers nomtitles label ///
posthead(" \\ & \multicolumn{8}{c}{Panel B:  Effect of Initial Rejection on Patent Grant} \\ \cmidrule(lr){2-9}") noobs ///
star(* 0.10 ** 0.05 *** 0.01) stat(mean r2_a fstat N clus control, fmt(2 2 2 0 0) label("Dependent Var. Mean" "Adj. R$^2$" "F-stat" "Observations" "\# of Clusters" "Art Unit $\times$ Year" )) 
		
	
